package org.nnxy.foodboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.nnxy.foodboot.entity.UserFavorite;

import java.util.List;
import java.util.Map;

/**
 * 用户收藏Mapper接口
 */
@Mapper
public interface UserFavoriteMapper extends BaseMapper<UserFavorite> {
    
    /**
     * 获取用户收藏的食谱ID列表
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 食谱ID列表
     */
    @Select("SELECT recipe_id FROM user_favorite WHERE user_id = #{userId} AND del_flag = '0' " +
            "ORDER BY favorite_time DESC LIMIT #{limit}")
    List<Integer> getFavoriteRecipeIds(@Param("userId") Integer userId, @Param("limit") Integer limit);
    
    /**
     * 获取某个食谱的收藏数量
     * @param recipeId 食谱ID
     * @return 收藏数量
     */
    @Select("SELECT COUNT(*) FROM user_favorite WHERE recipe_id = #{recipeId} AND del_flag = '0'")
    Integer getRecipeFavoriteCount(@Param("recipeId") Integer recipeId);
    
    /**
     * 获取收藏最多的食谱
     * @param limit 限制数量
     * @return 食谱列表
     */
    @Select("SELECT r.id, r.name, r.image_url, COUNT(uf.id) as favorite_count " +
            "FROM recipe r " +
            "JOIN user_favorite uf ON r.id = uf.recipe_id " +
            "WHERE r.del_flag = '0' AND uf.del_flag = '0' " +
            "GROUP BY r.id " +
            "ORDER BY favorite_count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getMostFavoritedRecipes(@Param("limit") Integer limit);
} 